<?php
	class user_model extends CI_Model {

		public function __construct()
		{
			// Call the Model constructor
			parent::__construct();
		}
		
		public function user_count() {
			return $this->db->count_all("users");
		}
		
		
		public function get_list_user($limit, $offset, $sort_by, $sort_order){
			$sort_order = ($sort_order == 'Asc') ? 'Asc' : 'Desc';
			$sort_columns = array('id','title','image','type', 'time_update');
			$sort_by = (in_array($sort_by,$sort_columns)) ? $sort_by : 'time_update';
			$q = $this->db->select("*",FALSE)
				->from("users")
				->limit($limit,$offset)
				->order_by($sort_by, $sort_order);
			$ret['rows'] = $q->get()->result();

			// Count Result
			$q = $this->db->select('COUNT(*) as count',FALSE)
				->from("users");
			$tmp = $q->get()->result();
			$ret['num_rows'] = $tmp[0]->count;

			return $ret;
		}
		
		public function get_list_user_by_time($startdate,$enddate) {
			$startdate =strtotime( date('Y/m/d 00:00:00',$startdate));
			$enddate =strtotime( date('Y/m/d 23:59:59',$enddate));			
			$this->db->select('*');
			$this->db->from('user');
			$this->db->where('time_update >', $startdate);
			$this->db->where('time_update <=', $enddate);
			$this->db->order_by("uid", "desc"); 
			$query = $this->db->get();		
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
			return 31;
		}
		
		public function get_list_all_user() {
			$this->db->select('*');
			$this->db->from('user');
			$this->db->order_by("uid", "desc"); 
			$query = $this->db->get();		
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
			return "empty";
		}
				
		public function get_user_by_id($id) {
			$q = $this->db->select('*',FALSE)
				->from("users")
				->where('uid', $id);
			$tmp = $q->get()->result();
			if($tmp!=null)
				return $tmp[0];
			else return 31;
		}
		public function insert_user($data,$filename)
		{
			//Kiểm tra trùng tên				
			if($data['name']=="")
			{
				return 1;//không tồn tại tên
			}
			else if(strlen($data['name'])<4)
			{
				return 2;
			}
			$q = $this->db->select("*",FALSE)
				->from("users")
				->where('name', $data['name']);
			$check = $q->get()->result();
			if($check!=null)
			{
				return 13;//Đã trùng tên
			}
			else
			{
				$insert_data = array(
					'name' => $data['name'],
					'fpayid' => $data['fpayid'],
					'description' => $data['description'],
					'time_update'=>time());
				$this->db->insert('user', $insert_data);
				return 200;
			}
		}
		
		public function update_user($data,$filename)
		{	
			//Kiểm tra trùng tên
			$q = $this->db->select("*",FALSE)
				->from("users")
				->where('uid !=', $data['uid'])
				->where('name', $data['name']);
			$check = $q->get()->result();
			if($check!=null)
			{
				return 13;//Đã trùng tên
			}
			else
			{
				$this->db->where('uid', $data['uid']);
				$update_data = array(
					'name' => $data['name'],
					'fpayid' => $data['fpayid'],
					'description' => $data['description'],
					'time_update'=>time());
				$this->db->update('user', $update_data);
				return 200;
			}		
		}
		
		
		public function delete_user($id)
		{		
			$this->db->delete('user', array('uid' =>$id));
			return 200;
		}	
}